<?php


namespace app\controller\api;


use app\model\AdminFeedbackRecord;
use app\model\AdminParents;
use app\model\AdminProblemArticle;
use app\model\AdminProblemType;
use app\model\AdminStaff;
use app\util\ReturnCode;
use think\facade\Validate;
use think\Response;

class Problem extends Base
{
    /**
     * @func type
     * @param type 类型分类 1--问题类型 2--反馈类型
     * @method GET
     * @desc 问题/反馈类型
     * @return Response
     * @Time: 2022/3/17 17:50
     */
    public function type(): Response {
        $type = $this->request->get('type', 1);
        $list = AdminProblemType::getTypeList($type);
        return $this->buildSuccess($list);
    }

    /**
     * @func list
     * @param problem_type_id 文章类型ID
     * @method GET
     * @desc 问题文章列表
     * @return Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @Time: 2022/3/17 17:54
     */
    public function list(): Response {
        $problem_type_id = $this->request->get('problem_type_id');
        $flag = $this->request->get('flag');

        $obj = (new AdminProblemArticle())->hasWhere('type', ['is_delete' => 0, 'type' => 1]);
        if ($flag) {
            $obj->where('flag', $flag);
        }
        if ($problem_type_id) {
            $obj->where('problem_id', $problem_type_id);
        }
        $list = $obj->field('AdminProblemArticle.id,AdminProblemArticle.title,flag')->select()->toArray();

        return $this->buildSuccess($list);
    }


    /**
     * @func detail
     * @param article_id 文章ID
     * @method GET
     * @desc 问题文章详情
     * @return Response
     * @Time: 2022/3/17 17:57
     */
    public function detail(): Response {
        $article_id = $this->request->get('article_id');
        $detail = AdminProblemArticle::where('id', $article_id)->field('title,content,update_time')->find()->toArray();
        if (!$detail) {
            return $this->buildFailed(ReturnCode::NOT_EXISTS, '问题详情不存在');
        }
        return $this->buildSuccess($detail);
    }

    /**
     * @func report
     * @method POST
     * @desc 意见反馈上报
     * @return Response
     * @Time: 2022/3/17 18:24
     */
    public function report(): Response {
        $postData = input('post.');
        $validate = Validate::rule([
            'phone|手机号' => 'mobile',
            'problem_type|问题类型' => 'require',
            'content|反馈意见和建议' => 'require',
            'uuid|用户标识' => 'require',
            'user_type|用户类型' => 'require|in:1,2',
            'screen_pic|问题截图' => 'array'
        ]);
        if (!$validate->check($postData)) {
            return $this->buildFailed(ReturnCode::PARAM_INVALID, $validate->getError());
        }
        $postData['content'] = strip_tags($postData['content']);
        if ($postData['user_type'] == 1) {
            $id = AdminStaff::where('uuid', $postData['uuid'])->value('id');
        } else {
            $id = AdminParents::where('uuid', $postData['uuid'])->value('id');
        }
        $postData['user_id'] = $id;
        $res = AdminFeedbackRecord::create($postData);
        if (!$res) {
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR);
        }
        return $this->buildSuccess();
    }
}